package com.qiyeji.real_estate.controller;

import com.qiyeji.real_estate.service.IOService;
import com.qiyeji.real_estate.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.Map;


@RequestMapping("/IO")
@RestController
@ResponseBody
public class IOController {
    @Autowired
    IOService ioService;

    @Value("./FileSystem")
    String path;//根目录

    @PostMapping("/mkdirFile")//创建项目资料文件
    public void mkdirFile(@RequestBody Map<String,String> params){
        String fileName=params.get("fileName");
        ioService.mkdirFile(fileName);
    }

    @PostMapping("/getSubfile")//获取子文件
    public Result getSubfile(@RequestBody Map<String,String> params) {
        File f=new File(path);
        String fileName = params.get("fileName");
//        f.getAbsolutePath()+"\\"+fileName
//        String[] list = ioService.getSubfile(fileName);
        String[] list = ioService.getSubfile(f.getAbsolutePath()+fileName);
        if (list == null) {
            return Result.error();
        } else {
            return Result.success(list);
        }
    }

    @PostMapping("/getParentfile")//获取父路径
    public Result getParentfile(@RequestBody Map<String,String> params){
        File f=new File(path);
        String fileName=params.get("fileName");
        String str=ioService.getParentfile(f.getAbsolutePath()+fileName);
        if (str == null) {
            return Result.error();
        } else {
            return Result.success(str);
        }
    }

    @PostMapping("/isFile")
    public Result isFile(@RequestBody Map<String,String> params){
        File f=new File(path);
        String fileName=params.get("fileName");
        boolean flag=ioService.isFile(f.getAbsolutePath()+fileName);
        if (flag == false) {
            return Result.error();
        } else {
            return Result.success(flag);
        }
    }

    @PostMapping("/isFolder")
    public Result isFolder(@RequestBody Map<String,String> params){
        File f=new File(path);
        String fileName=params.get("fileName");
        boolean flag=ioService.isFolder(f.getAbsolutePath()+fileName);
        if (flag == false) {
            return Result.error();
        } else {
            return Result.success(flag);
        }
    }
}
